﻿function PostVm(p) {
    var self = this;
    var parent = p;
    this.fileId = ko.observable();
    this.title = ko.observable();
    this.contents = ko.observable();
    this.time = ko.observable();
    this.author = ko.observable();
    this.categories = ko.observableArray();
    this.tags = ko.observableArray();
    this.permalink = ko.observable();
    this.isVisible = ko.observable(false);
    this.commentCount = ko.observable();
    this.commentText = ko.computed(function () {
        var count = this.commentCount();
        if (count == 0)
            return "No comments";
        if (count == 1)
            return "1 comment";
        if (count > 1)
            return count + " comments";
    }, this);
    this.updatedTime = ko.observable();

    this.isAuthor = ko.computed(function () {
        return parent.isAuthor();
    }, this);

    this.editLink = ko.computed(function () {
        return appPath + "Post/Compose/" + this.fileId();
    }, this);

    this.documents = ko.observableArray();
    this.comments = ko.observableArray();
    this.insertCommentModel = ko.observable();
    this.insertCommentMessage = ko.observable();

    this.showComments = ko.observable(false);
    this.showForm = ko.observable(false);

    this.load = function (id) {
        var url = appPath + 'Post/GetPost/' + id;      
        $.get(url, function (data) {
            self.loadJson(data);
        });
    };

    this.loadJson = function (data) {
        self.fileId(data.FileID);
        var insert = new CommentVm(self);
        insert.fileId(data.FileID);
        if (parent.isAuthor()) {
            insert.name(parent.authorName());
            insert.url(parent.authorUrl());
            insert.isAuthor(true);
        }
        self.insertCommentModel(insert);
        self.permalink(appPath + 'Post/' + data.FileID);
        self.title(data.Title);
        self.contents(data.HighlightedContents);
        self.time(data.FormattedTime);
        self.updatedTime(data.FormattedUpdatedTime);
        self.author(data.Author);
        self.commentCount(data.Comments.length);

        for (var idx = 0; idx < data.Categories.length; idx++) {
            var categoryVm = new CategoryVm(data.Categories[idx].CatID, data.Categories[idx].Name);
            self.categories.push(categoryVm);
        }

        for (var idx = 0; idx < data.Tags.length; idx++) {
            var tagVm = new TagVm(data.Tags[idx].TagId, data.Tags[idx].Name);
            self.tags.push(tagVm);
        }

        for (var idx = 0; idx < data.Comments.length; idx++) {
            var commentVm = new CommentVm(self);
            var comment = data.Comments[idx];
            commentVm.loadJson(comment);
            self.comments.push(commentVm);
        }

        for (var idx = 0; idx < data.Documents.length; idx++) {
            var documentVm = new DocumentVm();
            var document = data.Documents[idx];
            documentVm.path(document.Path);
            self.documents.push(documentVm);
        }

        self.isVisible(true);
    };

    this.viewComments = function () {
        this.showComments(!this.showComments());
    };

    this.addComment = function () {
        this.showForm(!this.showForm());
    };
}